package com.example.administrator.xxnapp.fragment.tucao;

import android.widget.Toast;

import com.example.administrator.xxnapp.App;
import com.example.administrator.xxnapp.bean.CommentListBean;
import com.example.administrator.xxnapp.bean.TuCaoBean;
import com.example.administrator.xxnapp.http.CustomStringCallback;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Went_Gone on 2018/4/28.
 */

public class TuCaoPresenter {
    private IViewTuCao mIView;
    private IModelTuCao mIModel;

    public TuCaoPresenter(IViewTuCao mIView, IModelTuCao mIModel) {
        this.mIView = mIView;
        this.mIModel = mIModel;
    }

    public void getTuCao(int page,int pageSize,int userId){
        mIModel.getTuCao(page,pageSize,userId).execute(new CustomStringCallback() {
            @Override
            public void onFinish() {
                super.onFinish();
                mIView.getTuCaoFinish();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                mIView.getTuCaoError();
            }

            @Override
            public void onSuccessString(Gson gson, String responseBody) {
                try {
                    JSONObject object = new JSONObject(responseBody);
                    if (object.getInt("result")==200){
                        Type type = new TypeToken<List<TuCaoBean>>() {
                        }.getType();
                        List<TuCaoBean> datas = gson.fromJson(object.getJSONArray("data").toString(),type);
                        if (datas == null){
                            datas = new ArrayList<>();
                        }
                        mIView.getTuCaoSuccess(datas);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void praise(int userId, int tucaoId, int praise, final int position) {
        mIModel.praise(userId,tucaoId,praise)
                .execute(new CustomStringCallback() {
                    @Override
                    public void onSuccessString(Gson gson, String responseBody) {
                        try {
                            JSONObject object = new JSONObject(responseBody);
                            if (object.getInt("result") == 200){
                                mIView.getPraiseSuccess(position);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void upComment(TuCaoBean tucaoBean,String content ,int userId,int atUserId, final int groupPosition){
        final CommentListBean bean = new CommentListBean();
        bean.setCommentUserId(userId);
        bean.setAtUserId(atUserId);
        bean.setCommentType(atUserId>0?atUserId:0);
        bean.setContent(content);
        bean.setTucaoId(tucaoBean.getId());
        mIModel.comment(bean)
                .execute(new CustomStringCallback() {
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onSuccessString(Gson gson, String responseBody) {
                        try {
                            JSONObject object = new JSONObject(responseBody);
                            if (object.getInt("result") == 200){
                                Type type = new TypeToken<CommentListBean>() {}.getType();
                                CommentListBean item = gson.fromJson(object.getJSONObject("data").toString(),type);
                                mIView.postCommentSuccess(item,groupPosition);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
